package com.framework.service.yhbservice.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.YHBDao.user.YhbCreditCardDao;
import com.framework.exception.RRException;
import com.framework.exception.menu.RespCode;
import com.framework.service.yhbservice.YhbCreditCardService;
import com.framework.service.yhbservice.YhbUserService;
import com.framework.utils.PackUtils;
import com.framework.yhbEntity.user.YhbCreditCard;
import com.framework.yhbEntity.user.YhbUser;
import com.framework.yhbEntity.view.CusTranView;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Transactional(rollbackFor = Exception.class)
@Service("yhbCreditCardService")
public class YhbCreditCardServiceImpl extends ServiceImpl<YhbCreditCardDao,YhbCreditCard> implements YhbCreditCardService {

    @Autowired
    private YhbUserService yhbUserService;


    @Override
    public List<Map<String,Object>> queryCardList(String token, String cardState, String parms, Integer pageNo) {
        if(pageNo == null){
            throw new RRException(RespCode.NOT_PARAM_ERROR);
        }
        pageNo = pageNo * 10;
        YhbUser user = yhbUserService.queryUserByToken(token);
        return baseMapper.queryCardList(user.getUserId(), cardState, parms, pageNo);
    }

    @Override
    public Map<String, Object> queryCardRewardMap(String token, String monthTime) {
        if(StringUtils.isBlank(monthTime)){
            throw new RRException(RespCode.NOT_PARAM_ERROR);
        }
        YhbUser user = yhbUserService.queryUserByToken(token);

        return baseMapper.queryCardRewardMap(user.getUserId(),monthTime);
    }

    @Override
    public void insertCard(YhbCreditCard yhbCreditCard) {
        //参数校验
        if(yhbCreditCard == null){
            throw new RRException(RespCode.NOT_PARAM_ERROR);
        }
        if(
                StringUtils.isBlank(yhbCreditCard.getProName()) ||
                yhbCreditCard.getBankType()==null ||
                StringUtils.isBlank(yhbCreditCard.getIdentity()) ||
                StringUtils.isBlank(yhbCreditCard.getPhone()) ||
                StringUtils.isBlank(yhbCreditCard.getUserId())){
            throw new RRException(RespCode.NOT_PARAM_ERROR);
        }
         YhbUser user= yhbUserService.queryUserByUserId(yhbCreditCard.getUserId());
        if(user == null){
            throw new RRException("9000","用户id错误");
        }

        //校验是否可以申请信用卡
        boolean isCard = queryIsCardRepeat(user.getUserId(),yhbCreditCard.getBankType(),yhbCreditCard.getPhone());
        if(!isCard){
            throw new RRException("9000","不能在同一代理商下重复申请信用卡");
        }
        yhbCreditCard.setProDate(new Date());
        yhbCreditCard.setCreateTime(new Date());
        yhbCreditCard.setUserId(user.getUserId());
        baseMapper.insert(yhbCreditCard);
    }

    @Override
    public boolean queryIsCardRepeat(String userId, Integer bankType, String phone) {
        if(StringUtils.isBlank(userId) ||
                bankType==null ||
                StringUtils.isBlank(phone)){
            throw new RRException(RespCode.NOT_PARAM_ERROR);
        }
        YhbUser user = yhbUserService.queryUserByUserId(userId);
        if(user == null){
            throw new RRException("9000","用户id错误");
        }
        return baseMapper.queryIsCardRepeat(userId,bankType,phone);
    }

    @Override
    public List<CusTranView> getCreditCardList(String token, Integer type, Integer pageNo) {
        if(type==null){
            throw new RRException("9000","参数错误,缺少类型");
        }
        if(pageNo==null || pageNo==0 ){
            throw new RRException("9000","参数错误,缺少分页参数");
        }
        YhbUser user = yhbUserService.queryUserByToken(token);
        Integer start=(pageNo-1)*10;
        List<Map<String,Object>> list=baseMapper.getCreditCardList(user.getUserId(),type,start);
        return PackUtils.packToCusTranView(list,"proDate");
    }

    @Override
    public BigDecimal queryBankAmount(Integer bankType) {
        return baseMapper.queryBankAmount(bankType);
    }

    @Override
    public List<CusTranView> getCreditCardListByUserId(String userId, Integer type, Integer pageNo) {
        if(type==null){
            throw new RRException("9000","参数错误,缺少类型");
        }
        if(pageNo==null || pageNo==0 ){
            throw new RRException("9000","参数错误,缺少分页参数");
        }
        YhbUser user = yhbUserService.queryUserByUserId(userId);
        Integer start=(pageNo-1)*10;
        List<Map<String,Object>> list=baseMapper.getCreditCardList(user.getUserId(),type,start);
        return PackUtils.packToCusTranView(list,"proDate");
    }

}
